<%--
  Created by IntelliJ IDEA.
  User: huangyanqiu
  Date: 2017/9/15
  Time: 14:15
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/resources.jsp" %>
<html>
<head>
    <title>团购专区配置列表</title>
</head>
<body>
<div>
    <div class="panel panel-default">
        <div class="panel-heading">
            <h3 class="panel-title">查询条件</h3>
        </div>
        <div class="panel-body">
            <form class="form-inline">
                <div class="form-group">
                    <label for="title">配置项名称：</label>
                    <input type="text" id="title" class="form-control" name="title" placeholder="请输入配置项名称">
                </div>
                <div class="form-group">
                    <label for="discount">折扣比例：</label>
                    <input type="text" id="discount" class="form-control" name="discount" placeholder="请输入折扣比例">
                </div>
                <button type="button" class="btn btn-info btn-search">查询</button>
                <button type="button" class="btn btn-default btn-reset">清空</button>
            </form>
        </div>
    </div>

    <div class="ems_table_content">
        <div class="panel panel-default">
            <div class="panel-heading">团购配置列表</div>
            <div id="table_tools">
                <div class="form-inline" role="form">
                    <button type="button" id="group_purchase_add" class="btn btn-outline btn-success">
                        <i class="glyphicon glyphicon-plus" aria-hidden="true"></i>
                        添加
                    </button>
                    <button type="button" id="group_purchase_edit" class="btn btn-outline btn-primary">
                        <i class="glyphicon glyphicon-edit" aria-hidden="true"></i>
                        修改
                    </button>
                    <button type="button" id="group_purchase_delete" class="btn btn-outline btn-danger">
                        <i class="glyphicon glyphicon-trash" aria-hidden="true"></i>
                        删除
                    </button>
                    <button type="button" id="group_purchase_enable" class="btn btn-outline btn-info">
                        <i class="glyphicon glyphicon-ok-sign" aria-hidden="true"></i>
                        启用
                    </button>
                    <button type="button" id="group_purchase_disable" class="btn btn-outline btn-warning">
                        <i class="glyphicon glyphicon-remove-sign" aria-hidden="true"></i>
                        冻结
                    </button>
                </div>
            </div>
            <table id="group_purchase_table" data-url="/ems/grouppurchase/pagination" data-show-export="true">
                <thead>
                <tr>
                    <th data-field="state" data-checkbox="true"></th>
                    <th data-field="title" class="col-sm-4">配置项名称</th>
                    <th data-field="lower_limit" class="col-sm-2">人数下限（含）</th>
                    <th data-field="upper_limit" class="col-sm-2">人数上限（含）</th>
                    <th data-field="discount" class="col-sm-1">折扣比例</th>
                    <th data-field="comment" class="col-sm-4">备注</th>
                    <th data-field="param_status" class="col-sm-2" data-formatter="statusFormat">状态</th>
                </tr>
                </thead>
            </table>
        </div>
    </div>
</div>

<script>

    // 创建弹出框
    var dialogOption = {
        width: "520px",
        height: "400px",
        url: "/ems/grouppurchase/edit",
        yesFn: function () {
            // 校验表单，通过后才可执行。
            if (myValidator.form()) {
                var result = groupPurchaseParaSave();
                if (result) {
                    search("#group_purchase_table", getQueryParams())
                };
                return result;
            }
        }
    }

    $(function () {
        // 创建列表
        $("#group_purchase_table").createTable();

        // 添加
        $("#group_purchase_add").on("click", function () {
            dialogOption.title = "新增团购配置";
            dialogOption.data = {};
            $("#group_purchase_add_dialog").openDialog(dialogOption);
        });

        // 编辑
        $("#group_purchase_edit").on("click", function () {
            var selectRows = $('#group_purchase_table').bootstrapTable('getSelections');
            if (selectRows.length == 0) {
                $.errorAlert("请选择要编辑的数据");
                return;
            }
            if (selectRows.length > 1) {
                $.errorAlert("暂时不支持多行数据编辑");
                return;
            }

            var rowId = selectRows[0].id;
            dialogOption.title = "编辑团购配置";
            dialogOption.data = {groupPurchaseParamId: rowId};
            $("#group_purchase_add_dialog").openDialog(dialogOption);
        })

        // 删除
        $("#group_purchase_delete").on("click", function () {
            var selectRows = $('#group_purchase_table').bootstrapTable('getSelections');
            if (selectRows.length == 0) {
                $.errorAlert("请选择要删除的数据");
                return;
            }

            var selectRowsList = "";
            for (var i = 0; i < selectRows.length; i++) {
                selectRowsList += selectRows[i].id + ",";
            }
            selectRowsList = selectRowsList.substring(0, selectRowsList.length - 1);

            $.confirmAlert("你确定要删除这些数据吗", function () {
                $.emsAjax({
                    data: {selectRowsList: selectRowsList},
                    type: "POST",
                    url: "/ems/grouppurchase/batchDelete",
                    success: function (resp) {
                        if (resp.status == "000") {
                            $.toast(resp.message, function () {
                                search("#group_purchase_table", getQueryParams());
                            });
                        } else {
                            $.errorAlert(resp.message);
                        }
                    }
                });
            });
        });

        // 启用
        $("#group_purchase_enable").on("click", function () {
            toggleGroupPurchaseStatus("enable")
        });

        // 冻结
        $("#group_purchase_disable").on("click", function () {
            toggleGroupPurchaseStatus("disable")
        });

        // 切换团购活动配置的状态
        function toggleGroupPurchaseStatus(status) {

            var selectRows = $('#group_purchase_table').bootstrapTable('getSelections');
            if (selectRows.length == 0) {
                $.errorAlert("请选择要操作的数据");
                return;
            }

            var selectRowsList = "";
            for (var i = 0; i < selectRows.length; i++) {
                selectRowsList += selectRows[i].id + ",";
            }
            selectRowsList = selectRowsList.substring(0, selectRowsList.length - 1);

            $.confirmAlert("您确定要操作这些数据吗", function () {
                $.emsAjax({
                    data: {
                        selectRowsList: selectRowsList,
                        paramStatus: status
                    },
                    type: "POST",
                    url: "/ems/grouppurchase/toggleGroupPurchaseStatus",
                    success: function (resp) {
                        if (resp.status == "000") {
                            $.toast(resp.message, function () {
                                search("#group_purchase_table", getQueryParams());
                            });
                        } else {
                            $.errorAlert(resp.message);
                        }
                    }
                });
            });
        }
    });

    // 状态格式化
    function statusFormat(srcValue) {
        if (srcValue == 0) {
            return "<span style='color: red;font-weight: bold' >冻结</span>";
        } else if (srcValue == 1) {
            return "<span style='color: #3cb371;font-weight: bold'>正常</span>";
        }
    }

</script>

<%-- 隐藏的弹出框 --%>
<div id="group_purchase_add_dialog"></div>
</body>
</html>
